package org.example.aaaatest.a1;


import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
import java.util.List;

public class Game {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎来到冒险游戏！");
        System.out.print("请输入你的英雄名称：");
        String heroName = scanner.nextLine();
        Player player = new Player(heroName);

        while (true) {
            System.out.println("\n" + player.getStatus());
            System.out.println("\n请选择操作：");
            System.out.println("1. 击败怪物");
            System.out.println("2. 查看状态");
            System.out.println("3. 退出游戏");
            System.out.println("4. 使用药水（恢复10HP）");
            System.out.print("请输入选项：");
            String choice = scanner.nextLine();

            if (choice.equals("3")) {
                System.out.println("感谢游玩！");
                break;
            } else if (choice.equals("1")) {
                Monster monster = Monster.generateMonster(player.level);
                System.out.println("\n野生的 " + monster.name + " 出现了！");
                System.out.println(monster.getStatus());

                while (!player.isDefeated() && !monster.isDefeated()) {
                    // 玩家回合
                    player.attack(monster);
                    if (monster.isDefeated()) {
                        System.out.println(monster.name + " 被击败了！");
                        player.gainExperience(monster.level * 5);
                        // 掉落药水
                        if (Math.random() < 0.3) {
                            player.potions += 1;
                            System.out.println("你获得了一瓶药水！");
                        }
                        // 显示经验差值
                        int nextLevelExp = player.level * player.level * 5;
                        System.out.println("距离下一级还需 " + (nextLevelExp - player.exp) + " 点经验！");
                        break;
                    }

                    // 怪物回合
                    monster.attack(player);
                    if (player.isDefeated()) {
                        System.out.println("你被击败了... 游戏结束！");
                        System.exit(0);
                    }
                }
            } else if (choice.equals("2")) {
                System.out.println(player.getStatus());
            } else if (choice.equals("4")) {
                if (player.potions > 0) {
                    player.currentHP += 10;
                    if (player.currentHP > player.maxHP) player.currentHP = player.maxHP;
                    player.potions--;
                    System.out.println("你使用了一瓶药水，恢复10点HP！");
                } else {
                    System.out.println("你没有药水！先击败怪物获取药水。");
                }
            } else {
                System.out.println("无效选项，请重试。");
            }
        }

        scanner.close();
    }

    // ———————————————— Character 类 ————————————————
    static class Character {
        protected String name;
        protected int level;
        protected int exp;
        protected int currentHP;
        protected int maxHP;
        protected int minAttack;
        protected int maxAttack;
        protected int defense;
        protected double critChance = 0.2;     // 20% 暴击率
        protected double critMultiplier = 1.5;  // 暴击伤害倍数
        protected double dodgeChance = 0.15;    // 15% 闪避率
        protected List<Skill> skills = new ArrayList<>();
        protected boolean isStunned = false;

        public Character(String name, int level, int maxHP, int minAttack, int maxAttack, int defense) {
            this.name = name;
            this.level = level;
            this.maxHP = maxHP;
            this.currentHP = maxHP;
            this.minAttack = minAttack;
            this.maxAttack = maxAttack;
            this.defense = defense;
            this.exp = 0;
        }

        public void attack(Character opponent) {
            if (isStunned) {
                System.out.println(name + " 被眩晕，无法行动！");
                isStunned = false;
                return;
            }

            if (Math.random() < opponent.dodgeChance) {
                System.out.println(opponent.name + " 闪避了 " + name + " 的攻击！");
                return;
            }

            // 技能使用频率提升至50%
            if (Math.random() < 0.5 && !skills.isEmpty()) {
                Skill skill = skills.get((int)(Math.random() * skills.size()));
                skill.cast(this, opponent);
            } else {
                Random random = new Random();
                int baseDamage = random.nextInt(maxAttack - minAttack + 1) + minAttack;
                int damage = baseDamage;
                if (Math.random() < critChance) {
                    damage = (int)(baseDamage * critMultiplier);
                    System.out.println(name + " 发动暴击，造成了 " + damage + " 点伤害！");
                } else {
                    System.out.println(name + " 攻击了 " + opponent.name + "，造成了 " + damage + " 点伤害！");
                }
                // 应用防御
                int finalDamage = damage - opponent.defense;
                if (finalDamage < 0) finalDamage = 0;
                opponent.takeDamage(finalDamage);
            }
        }

        public void takeDamage(int damage) {
            currentHP -= damage;
            if (currentHP < 0) currentHP = 0;
        }

        public boolean isDefeated() {
            return currentHP <= 0;
        }

        public void gainExperience(int expGained) {
            System.out.println(name + " 获得了 " + expGained + " 点经验！");
            this.exp += expGained;
            checkLevelUp();
        }

        protected void checkLevelUp() {
            int requiredExp = level * level * 5;
            while (exp >= requiredExp) {
                System.out.println(name + " 升级了！达到了 " + (level + 1) + " 级！");
                level++;
                maxHP += 5;
                currentHP = maxHP;
                minAttack += 2;
                maxAttack += 2;
                defense += 1;
                requiredExp = level * level * 5;
            }
        }

        public String getStatus() {
            return "名称: " + name + " | 等级: " + level + " | HP: " + currentHP + "/" + maxHP +
                    " | 攻击力: " + minAttack + "-" + maxAttack + " | 防御: " + defense;
        }
    }

    // ———————————————— Player 类 ————————————————
    static class Player extends Character {
        public int potions = 0;

        public Player(String name) {
            super(name, 1, 25, 4, 6, 2); // 攻击力4-6，防御2
        }

        @Override
        public String getStatus() {
            return super.getStatus() + " | 药水: " + potions;
        }
    }

    // ———————————————— Monster 类 ————————————————
    static class Monster extends Character {
        private static final String[] MONSTER_TYPES = {
                "哥布林",
                "恶魔", "巨狼", "火蜥蜴", "毒蝎"
        };

        public Monster(String name, int level) {
            // 怪物HP公式：level*6+10，攻击力区间：level*2到 level*3，防御：level
            super(name, level, level * 6 + 10, level * 2, level * 3, level);
            assignSkills(name);
        }

        public static Monster generateMonster(int playerLevel) {
            int monsterLevel = playerLevel;
            String name = MONSTER_TYPES[(int)(Math.random() * MONSTER_TYPES.length)];
            return new Monster(name, monsterLevel);
        }

        private void assignSkills(String name) {
            switch (name) {
                case "恶魔":
                    skills.add(new Fireball());
                    break;
                case "巨狼":
                    skills.add(new Frenzy());
                    break;
                case "火蜥蜴":
                    skills.add(new Burn());
                    break;
                case "毒蝎":
                    skills.add(new PoisonSting());
                    skills.add(new Slow());
                    break;
            }
        }
    }

    // ———————————————— 技能类 ————————————————
    static abstract class Skill {
        protected String name;
        protected String description;

        public Skill(String name, String description) {
            this.name = name;
            this.description = description;
        }

        public abstract void cast(Character caster, Character target);
    }

    static class Fireball extends Skill {
        public Fireball() { super("火球术", "造成额外火焰伤害"); }

        @Override
        public void cast(Character caster, Character target) {
            Random random = new Random();
            int baseDamage = random.nextInt(caster.maxAttack - caster.minAttack + 1) + caster.minAttack;
            int fireDamage = (int)(baseDamage * 1.5);
            int finalDamage = fireDamage - target.defense;
            if (finalDamage < 0) finalDamage = 0;
            System.out.println(caster.name + " 使用了技能 [火球术]，对 " + target.name + " 造成了 " + finalDamage + " 点火焰伤害！");
            target.takeDamage(finalDamage);
        }
    }

    static class PoisonSting extends Skill {
        public PoisonSting() { super("毒刺", "每回合持续掉血"); }

        @Override
        public void cast(Character caster, Character target) {
            int poisonDamage = 5;
            int finalDamage = poisonDamage - target.defense;
            if (finalDamage < 0) finalDamage = 0;
            System.out.println(caster.name + " 使用了技能 [毒刺]，对 " + target.name + " 造成了持续 " + finalDamage + " 点毒伤！");
            target.takeDamage(finalDamage);
        }
    }

    static class Frenzy extends Skill {
        public Frenzy() { super("狂怒", "连续攻击两次"); }

        @Override
        public void cast(Character caster, Character target) {
            System.out.println(caster.name + " 进入狂怒状态！");
            caster.attack(target);
            caster.attack(target);
        }
    }

    static class Burn extends Skill {
        public Burn() { super("灼烧", "造成额外灼烧伤害"); }

        @Override
        public void cast(Character caster, Character target) {
            Random random = new Random();
            int baseDamage = random.nextInt(caster.maxAttack - caster.minAttack + 1) + caster.minAttack;
            int burnDamage = (int)(baseDamage * 1.2);
            int finalDamage = burnDamage - target.defense;
            if (finalDamage < 0) finalDamage = 0;
            System.out.println(caster.name + " 使用了技能 [灼烧]，对 " + target.name + " 造成了 " + finalDamage + " 点灼烧伤害！");
            target.takeDamage(finalDamage);
        }
    }

    static class Stun extends Skill {
        public Stun() { super("眩晕", "使目标无法行动一回合"); }

        @Override
        public void cast(Character caster, Character target) {
            System.out.println(caster.name + " 使用了技能 [眩晕]，使 " + target.name + " 眩晕！");
            target.isStunned = true;
        }
    }

    static class Slow extends Skill {
        public Slow() { super("减速", "降低目标攻击力"); }

        @Override
        public void cast(Character caster, Character target) {
            int slowAmount = 2;
            System.out.println(caster.name + " 使用了技能 [减速]，使 " + target.name + " 攻击力减少 " + slowAmount + " 点！");
            target.maxAttack -= slowAmount;
            target.minAttack -= slowAmount;
        }
    }
}
